package br.unisinos.ubicomp.locationawaretaskmanager;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TaskArrayAdapter extends ArrayAdapter<Task> {

	private List<Task> list;
	private final Activity context;

	public TaskArrayAdapter(Activity context, List<Task> list) {
		super(context, R.layout.task_row, list);
		this.context = context;
		this.list = list;
	}

	static class ViewHolder {
		protected TextView text;
	}
	
	public void setList(List<Task> list) {
		this.list = list;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LinearLayout listView;
		// Get the current task
		Task task = list.get(position);

		// Inflate the view
		if (convertView == null) {
			listView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi;
			vi = (LayoutInflater) getContext().getSystemService(inflater);
			vi.inflate(R.layout.task_row, listView, true);
		} else {
			listView = (LinearLayout) convertView;
		}
		// Get the text boxes from the row .xml file
		TextView alertText = (TextView) listView
				.findViewById(R.id.taskName);
		TextView alertDate = (TextView) listView
				.findViewById(R.id.taskInfo);

		// Insert real dates to textView
		alertText.setText(task.getName());
		alertDate.setText(task.getInfo());

		return listView;
	}
	
}